package ltd.hxya.novel.book.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ltd.hxya.novel.book.entity.Book;
import ltd.hxya.novel.book.entity.BookCategory;
import ltd.hxya.novel.book.entity.BookIndex;
import ltd.hxya.novel.book.service.IBookCategoryService;
import ltd.hxya.novel.book.service.IBookIndexService;
import ltd.hxya.novel.book.service.IBookService;
import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.common.utils.BaseUtils;
import ltd.hxya.novel.common.valid.UpdateOperate;
import ltd.hxya.novel.entity.rowdata.DataPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/book/admin")
public class AdminController {

    @Autowired
    private IBookService bookService;

    @Autowired
    private IBookCategoryService bookCategoryService;

    @Autowired
    private IBookIndexService bookIndexService;

    /**
     * 根据小说查询条件，查出小说列表，并分页
     * @param book
     * @return
     */
    @GetMapping("/book/list")
    public Result<Page<Book>> bookList(Book book){
        Page<Book> bookPage = bookService.listBook(book);
        return Result.success(bookPage);
    }

    /**
     * 根据分类查询条件，查出分类列表，并分页
     */
    @GetMapping("/category/list")
    public Result<Page<BookCategory>> bookCategory(BookCategory bookCategory){
        Page<BookCategory> categoryPage = bookCategoryService.listBookCategory(bookCategory);
        return Result.success(categoryPage);
    }


    //查询不是该分类下的小说,并分页
    @GetMapping("/category/uncategoryBook/{categoryId}")
    public Result<Page<Book>> uncategoryBook(@PathVariable Long categoryId){
        Page<Book> bookPage = bookService.uncategoryBook(categoryId);
        return Result.success(bookPage);
    }

    //根据小说分类禁用小说
    @PostMapping("/disableBookByCategory/{categoryId}")
    public Result disableBookByCategory(@PathVariable Integer categoryId){
        bookService.disableBookByCategory(categoryId);
        return Result.success();
    }

    //将某本小说添加到某个分类下
    @PostMapping("/category/addBook")
    public Result addBook(@RequestParam String bookId,@RequestParam String categoryId){
        bookService.addBook(bookId,categoryId);
        return Result.success();
    }

    //改变小说状态
    @PostMapping("/category/changeBookStatus")
    public Result changeBookStatus(@RequestParam("id") String id ,@RequestParam("status") Boolean status){
        bookService.changeBookStatus(id,status);
        return Result.success();
    }

    //添加分类
    @PutMapping("/category/add")
    public Result addCategory(@RequestBody BookCategory bookCategory){
        bookCategoryService.addCategory(bookCategory);
        return Result.success();
    }

    //修改分类
    @PostMapping("/category/update")
    public Result updateCategory(@RequestBody @Validated(value = UpdateOperate.class) BookCategory bookCategory){
        bookCategoryService.updateCategory(bookCategory);
        return Result.success();
    }

    //获取当前分类
    @GetMapping("/book/currentCategory/{bookId}")
    public Result<Book> current(@PathVariable Long bookId){
        Book book = bookService.getById(bookId);
        return Result.success(book);
    }

    //查询不是当前小说的分类
    @GetMapping("/book/unaddCategory/{bookId}")
    public Result<Page<BookCategory>> unaddCategory(@PathVariable String bookId){
        Page<BookCategory> bookCategoryPage = bookService.unaddCategory(bookId);
        return Result.success(bookCategoryPage);
    }

    //根据id查询出小说的分类
    @GetMapping("/category/bookCategoryById/{categoryId}")
    public Result<BookCategory> bookCategoryById(@PathVariable("categoryId") Integer categoryId){
        BookCategory bookCategory = bookCategoryService.getById(categoryId);
        return Result.success(bookCategory);
    }

    //修改小说信息
    @PostMapping("/book/updateInfo")
    public Result updateInfo(@RequestBody Book book){
        bookService.updateById(book);
        return Result.success();
    }

    /**
     * 添加小说以及小说的相关信息
     * @return
     */
    @PutMapping("/book/saveBookList")
    public Result saveBook(@RequestBody List<DataPart> dataParts){
        bookService.saveBook(dataParts);
        return Result.success();
    }

    //根据id删除小说
    @DeleteMapping("/book/deleteById")
    public Result deleteById(String[] ids){
        bookService.deleteById(ids);
        return Result.success();
    }

    //修改小说章节的收费情况
    @PostMapping("/changeIsVip")
    public Result changeIsVip(@RequestBody BookIndex bookIndex){
        bookIndexService.changeIsVip(bookIndex);
        return Result.success();
    }

    //删除某个分类
    @DeleteMapping("/category/delete")
    public Result deleteCategory(String[] ids){
        bookCategoryService.deleteBatchCategory(ids);
        return Result.success();
    }

    //删除小说的某一个或多个章节
    @DeleteMapping("/deleteIndecies")
    public Result deleteIndecies(String bookId,String[] indexIds){
        bookIndexService.deleteIndecies(bookId,indexIds);
        return Result.success();
    }

    //清空某一个小说的所有章节
    @DeleteMapping("/clearBookIndex/{bookId}")
    public Result clearBookIndex(@PathVariable("bookId") String bookId){
        bookIndexService.clearBookIndex(bookId);
        return Result.success();
    }


    @PostMapping("/updateBook")
    public Result updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        return Result.success();
    }

}
